package bkone;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class deletebbookk
 */
@WebServlet("/deletebbookk")
public class deletebbookk extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String JDBC_URL = "jdbc:mysql://localhost:3309/blog?useUnicode=true&characterEncoding=utf8";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "ZLWL";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public deletebbookk() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: deletebbookk").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		String deletebbookkid = request.getParameter("deletebbookkid");
		System.out.println("deletebbookkid:" + deletebbookkid);
		try {
		    Class.forName("com.mysql.jdbc.Driver").newInstance();
	    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
		    e.printStackTrace();
	    }
	    Connection cont6 = null;
	    PreparedStatement prestn6 = null;
	    try {
	    	cont6 = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);
	    	Statement stmt6 = cont6.createStatement();
		    String sctpye = "delete from blog where id = ?";
		    prestn6 = cont6.prepareStatement(sctpye);
		    prestn6.setString(1, deletebbookkid);
		    int setpinglshul = 0;
		    if (!deletebbookkid.equals("")) {
		    	prestn6.executeUpdate();
			    String sqlshul = "select count(id) total from review where bei_id = " + deletebbookkid;
			    ResultSet rsshul = stmt6.executeQuery(sqlshul);
			    while (rsshul.next()) {
				    int getpinglshul = rsshul.getInt(1);
				    setpinglshul = getpinglshul;
			    }
			    System.out.println("the count of pl:" + setpinglshul);
			    rsshul.close();
		    }
			String scsuoypingl = "delete from review where bei_id = " + deletebbookkid + " order by id limit ?";
		    prestn6 = cont6.prepareStatement(scsuoypingl);
		    prestn6.setInt(1, setpinglshul);
		    if (!deletebbookkid.equals("")) {
		    	prestn6.executeUpdate();
			}
			stmt6.close();
		    prestn6.close();
		    cont6.close();
            response.sendRedirect("/admin_bloggl.jsp");
	    } catch (SQLException e) {
		    e.printStackTrace();
	    }
	}

}
